<?php 
/**
	Class used to manage the rating
*/
require_once('Class/connexionDB.Class.php');

class rating
{
	var $date_call = '';
	var $date_eval = '';
	var $n_akio = '';
	var $n_order = '';
	var $name_rater = '';
	var $name_consultant = '';
	var $final_mark = '';
	var $optional_mark = '';
	var $improvement = '';
	var $id_topic = 0;
	var $id_eval_type = '';
	var $id_groups = array();
	var $listedCriteria = array();
	var $insertedID = 0;
	var $id_country = 0;
	var $id_recipient = 0;
	var $team = 'N/A';
	var $id_evaluation_form = 0;
	var $validation = 0;
	var $listedMarks;
	var $id_account_rater = 0;

	static function isValidDate($date, $format = 'd/m/Y')
	{
		$d = DateTime::createFromFormat($format, $date);
		return $d && $d->format($format) == $date;
	}

	static function isValidNAkio($n_akio)
	{
		if (isset($n_akio) && $n_akio != '' && is_numeric($n_akio))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	static function isValidNOrder($n_order)
	{
		if (isset($n_order) && $n_order != '' && is_numeric($n_order))
		{
			return true;
		}
		else 
		{
			return false;
		}
	}

	static function isValidName($name)
	{
		if (isset($name) && $name != '')
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	static function isValidMark($mark)
	{
		if (isset($mark) && $mark != '' && is_numeric($mark) && $mark <= 100)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	static function isValidComment($comment)
	{
		if (isset($comment) && $comment != '')
		{
			return true;
		}
		else 
		{
			return false;
		}
	}

	static function isValidIdTopic($id)
	{
		if (isset($id) && $id != '' && is_numeric($id))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	static function isValidIdEvalType($id)
	{
		if (isset($id) && $id != '')
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	function isValidData($data)
	{
		$errors = array();

		if ($this->isValidDate($data['date_call']))
		{
			$this->date_call = date('Y-m-d', strtotime(str_replace('/', '-', $data['date_call'])));
		}

		if ($this->isValidDate($data['date_eval']))
		{
			$this->date_eval = date('Y-m-d', strtotime(str_replace('/', '-', $data['date_eval'])));
		}

		if ($this->isValidNAkio($data['n_akio']))
		{
			$this->n_akio = $data['n_akio'];
		}

		if ($this->isValidNAkio($data['n_order']))
		{
			$this->n_order = $data['n_order'];
		}

		if($this->isValidName($data['name_rater']))
		{
			$this->name_rater = $data['name_rater'];
		}

		if($this->isValidName($data['name_consultant']))
		{
			$this->name_consultant = $data['name_consultant'];
		}

		if($this->isValidMark($data['final_mark']))
		{
			$this->final_mark = intval($data['final_mark']);
		}

		if($this->isValidMark($data['optional_mark']))
		{
			$this->optional_mark = $data['optional_mark'];
		}

		if ($this->isValidComment($data['improvement']))
		{
			$this->improvement = $data['improvement'];
		}

		if (isset($data['topic']) && $this->isValidIdTopic($data['topic']))
		{
			$this->id_topic = $data['topic'];
		}

		if ($this->isValidIdEvalType($data['account_type']))
		{
			$this->id_eval_type = $data['account_type'];
		}

		if (isset($data['country']) && $data['country'] != '')
		{
			$this->id_country = intval($data['country']);
		}
		
		if (isset($data['recipient']) && $data['recipient'] != '')
		{
			$this->id_recipient = intval($data['recipient']);
		}

		if (isset($data['team']) && $data['team'] != '')
		{
			$this->team = $data['team'];
		}

		if (isset($data['id_eval_form']) && $data['id_eval_form'] != '')
		{
			$this->id_evaluation_form = intval($data['id_eval_form']);
		}

		if (isset($data['selectedValidation'])  && $data['selectedValidation'] != '')
		{
			$this->validation = $data['selectedValidation'];
		}

		if (isset($data['id_account']) && $data['id_account'] != '')
		{
			$this->id_account_rater = intval($data['id_account']);
		}

		return true;
	}

	function setValidGroup($data)
	{
		foreach ($data as $key => $value)
		{
			if (strpos($key, 'group') !== false)
			{
				$this->id_groups[] = $value;
			}
		}
	}

	function setValidCriteria($data)
	{
		$idx = 0;
		
		foreach ($data as $key => $value)
		{
			if (strpos($key, 'criteria') !== false && strpos($key, 'unacceptable') === false)
			{
				$id = explode('_', $key);
				$id = $id[1];
				
				if (isset($data['criteria_' . $id]))
				{
					$this->listedCriteria[$id]['id_mark_criteria'] = $data['criteria_' . $id];
				}
				if (isset($data['commentary_' . $idx]))
				{
					$this->listedCriteria[$id]['commentary'] = $data['commentary_' . $idx];
					$idx++;
				}
				if (isset($data[$key. '_unacceptable']))
				{
					$this->listedCriteria[$id]['unacceptable'] = intval($data[$key. '_unacceptable']);
				}
				else
				{
					$this->listedCriteria[$id]['unacceptable'] = 0;	
				}
			}
		}
	}

	function addNewRating()
	{
		$db = new connexionDB();
		$this->insertedID = $db->addNewRating(get_object_vars($this));
		if ($this->insertedID != 0)
		{
			$db->addNewRatingCriteria($this->insertedID, $this->listedCriteria);
		}
	}

	/**
		Insert a new rating in data base
	*/
	function createNewRating($data)
	{
		if ($this->isValidData($data) == true)
		{
			$this->setValidGroup($data);
			$this->setValidCriteria($data);
			$this->addNewRating();
		}
		return $this->insertedID;
	}

	/**
		Return 
	*/
	function getRatingFromID($id)
	{
		$query = 'SELECT 
					`rating`.`id`,
					`rating`.`progress`,
					`rating`.`id_account_rater`,
					`rating`.`date_call`,
					`rating`.`reject_debrief`,
					`rating`.`date_eval`,
					`rating`.`date_last_edit`,
					`rating`.`n_akio`,
					`rating`.`n_order`,
					`rating`.`name_rater`,
					`rating`.`name_consultant`,
					`rating`.`final_mark`,
					`rating`.`optional_mark`,
					`rating`.`improvement`,
					`rating`.`id_country`,
					`rating`.`id_recipient`,
					`rating`.`id_topic`,
					`rating`.`eval_type`,
					`rating`.`groups`,
					`rating`.`team`,
					`rating`.`id_evaluation_form`,
					`rating`.`validation`,
					`evaluation_form`.`name` AS `eval_name`
					FROM 
						`rating` LEFT JOIN
						`evaluation_form` ON `evaluation_form`.`id`=`rating`.`id_evaluation_form`
					WHERE
						`rating`.`id`=' . $id;
		$db = new connexionDB();

		$result = $db->executeQuery($query);
		if (isset($result[0]))
		{
			return $result[0];
		}
		return array();
	}

	function getRatingFromMultipleID($whereClause)
	{
		$query = 'SELECT 
					`rating`.`id`,
					`rating`.`date_call`,
					`rating`.`date_eval`,
					`rating`.`n_akio`,
					`rating`.`n_order`,
					`rating`.`name_rater`,
					`rating`.`name_consultant`,
					`rating`.`final_mark`,
					`rating`.`optional_mark`,
					`rating`.`improvement`,
					`rating`.`id_country`,
					`rating`.`id_recipient`,
					`rating`.`id_topic`,
					`rating`.`eval_type`,
					`rating`.`groups`,
					`rating`.`team`,
					`rating`.`id_evaluation_form`
					FROM 
						`rating`
					WHERE
						'. $whereClause;
		$db = new connexionDB();
		
		$result = $db->executeQuery($query);
		if (isset($result))
		{
			return $result;
		}
		return array();
	}

	/**
		Get all the old marks saved
	*/
	function getOldMark($id_rating)
	{
		$db 		= new connexionDB();
		$id_rating 	= intval($id_rating);
		$str 		= '';

		$this->listedMarks = $db->getOldMark($id_rating);
		if (!empty($this->listedMarks))
		{
			$str = $this->htmlDisplayMarks();	
		}
		
		return $str;
	}

	function htmlDisplayMarks()
	{
		$str 	= '';
		$i 		= 0;

		foreach ($this->listedMarks as $value)
		{
			if ($i != 0)
			{
				$str .= ' - ';
			}
			$str .= $value['old_mark'] . '/100';
			$i++;
		}
		return $str;
	}
}

?>